package com.wmz.disruptor.producer;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import com.wmz.disruptor.model.LongEvent;

/**
 * @author lihechun
 * @version 1.0
 * @description: 事件转换器：省了从 环形队列 获取 序号， 然后拿到事件 填充数据， 再发布序号 中的第二步骤
给 事件 填充 数据 的动作，在 EventTranslatorOneArg 完成 （LongEventProducer替代）
 * @date 2024/11/28 23:35
 */
public class LongEventProducerWithTranslator {
    private final RingBuffer<LongEvent> ringBuffer;

    public LongEventProducerWithTranslator(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }
    //一个translator可以看做一个事件初始化器，publicEvent方法会调用它
    //填充Event
    private static final EventTranslatorOneArg<LongEvent, Long> TRANSLATOR =
        new EventTranslatorOneArg<LongEvent, Long>() {
            public void translateTo(LongEvent event, long sequence, Long data) {
                event.setValue(data);
            }
        };

    public void onData(Long data) {
        ringBuffer.publishEvent(TRANSLATOR, data);
    }
}
